package com.sdtv.basemodule.utils;

import android.view.View;

import com.blankj.utilcode.util.ToastUtils;

/**
 * @ProjectName: MediunFusion
 * @Package: com.sdtv.basemodule.utils
 * @ClassName: ClickProxy
 * @Description: 避免连续点击事件
 * @Author: 小郭同学
 * @CreateDate: 2019-07-16 08:57
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class ClickProxy implements View.OnClickListener {

    //设置连续点击的时间间隔 默认2000ms
    private long timeInterval = 1000;
    //连续点击事件
    private Again again;
    //当前时间
    private long lastClick = System.currentTimeMillis();
    //点击事件
    private View.OnClickListener originalClick;

    /**
     * 构造器
     */
    public ClickProxy(View.OnClickListener onClickListener) {
        this(onClickListener, 1000);
    }

    public ClickProxy(View.OnClickListener onClickListener, long timeInterval) {
        this(onClickListener, timeInterval, null);
    }


    public ClickProxy(View.OnClickListener onClickListener, long timeInterval, Again again) {
        this.timeInterval = timeInterval;
        this.again = again;
        this.originalClick = onClickListener;
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        if (System.currentTimeMillis() - lastClick < timeInterval) {
            //当前连续点击了--联系点击事件
            ToastUtils.showShort("点击太频繁了哟");
            if (null != again) {
                again.doAgain();
            }
        } else {
            // 没有连续点击 执行事件
            originalClick.onClick(view);
            lastClick = System.currentTimeMillis();
        }
    }


    /**
     * 连续点击--执行的事件
     */
    public interface Again {

        void doAgain();
    }
}
